//
//  GPVideoToolView.m
//  Keping
//
//  Created by 23 on 2017/12/10.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPVideoToolView.h"
#import "KLVideoCallButton.h"

@interface GPVideoToolView ()

/**当前状态*/
@property(nonatomic,assign) GPVideoViewStatus status;

#pragma mark - 未接通状态
/**主叫 -- 取消呼叫*/
@property (weak, nonatomic) IBOutlet KLVideoCallButton *cancelButton;
/**被叫 -- 拒绝*/
@property (weak, nonatomic) IBOutlet KLVideoCallButton *declineButton;
/**被叫 -- 接收*/
@property (weak, nonatomic) IBOutlet KLVideoCallButton *reveiveButton;


#pragma mark - 接通状态
/**通话中 -- 左边菜单*/
@property (weak, nonatomic) IBOutlet UIButton *leftButton;
/**通话中 -- 挂掉*/
@property (weak, nonatomic) IBOutlet KLVideoCallButton *handupButton;
/**通话中 -- 右边菜单*/
@property (weak, nonatomic) IBOutlet UIButton *rightButton;


#pragma mark - 记录属性
/**是否开免提*/
@property(nonatomic,assign) BOOL isHandsOff;
/**是否开静音*/
@property(nonatomic,assign) BOOL isSlience;

@end


@implementation GPVideoToolView

+ (instancetype)toolViewWithStatus:(GPVideoViewStatus)status
{
    GPVideoToolView *toolView = nil;
    
    //加载不同的布局
    switch (status) {
        case GPVideoViewStatusCalling:
        case GPVideoViewStatusCalled:
        {
            //主叫+被叫+未接通
            toolView = [[self alloc] initWithStatus:GPVideoViewStatusCalled];
        }
            break;
        case GPVideoViewStatusVideoSession:
        case GPVideoViewStatusVoiceSession:
        {
            //音频+接通中
            toolView = [[self alloc] initWithStatus:GPVideoViewStatusVideoSession];
        }
            break;
        default:
            break;
    }
    
    //根据状态更新UI
    [toolView updateUIWithStatus:status];
    toolView.status = status;
    
    return toolView;
}

- (instancetype)initWithStatus:(GPVideoViewStatus )status
{
    if (self = [super init]) {
        
        //创建元素
        switch (status) {
            case GPVideoViewStatusCalling:
            case GPVideoViewStatusCalled:
            {
                //未接通状态(主叫+被叫)
                
                //--- 主叫(取消)
                KLVideoCallButton *cancelButton = [[KLVideoCallButton alloc] init];
                [cancelButton setImage:[UIImage imageNamed:@"ic_call_reject"] forState:UIControlStateNormal];
                [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
                [cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:cancelButton];
                self.cancelButton = cancelButton;
                
                //--- 被叫(拒绝)
                KLVideoCallButton *declineButton = [[KLVideoCallButton alloc] init];
                [declineButton setImage:[UIImage imageNamed:@"ic_call_reject"] forState:UIControlStateNormal];
                [declineButton setTitle:@"Decline" forState:UIControlStateNormal];
                [declineButton addTarget:self action:@selector(rejectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:declineButton];
                self.declineButton = declineButton;
                
                //--- 被叫(接收)
                KLVideoCallButton *receiveButton = [[KLVideoCallButton alloc] init];
                [receiveButton setImage:[UIImage imageNamed:@"ic_call_receive"] forState:UIControlStateNormal];
                [receiveButton setTitle:@"Receive" forState:UIControlStateNormal];
                [receiveButton addTarget:self action:@selector(receiveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:receiveButton];
                self.reveiveButton = receiveButton;
                
                //设置约束
                [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.centerY.mas_equalTo(self);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(90);
                }];
                [declineButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(90);
                    make.left.mas_equalTo(self).offset(30);
                }];
                [receiveButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(self);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(90);
                    make.right.mas_equalTo(self).offset(-30);
                }];
                
                
            }
                break;
                
            case GPVideoViewStatusVoiceSession:
            case GPVideoViewStatusVideoSession:
            {
                //接通状态(视频+音频)
                
                KLVideoCallButton *hangupButton = [[KLVideoCallButton alloc] init];
                [hangupButton setImage:[UIImage imageNamed:@"ic_call_reject"] forState:UIControlStateNormal];
                [hangupButton setTitle:@"Hang Up" forState:UIControlStateNormal];
                [hangupButton addTarget:self action:@selector(hangupButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:hangupButton];
                self.handupButton = hangupButton;
                
                UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [leftButton addTarget:self action:@selector(leftButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:leftButton];
                [leftButton sizeToFit];
                self.leftButton = leftButton;
                
                UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
                [rightButton addTarget:self action:@selector(rightButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:rightButton];
                [rightButton sizeToFit];
                self.rightButton = rightButton;
                
                
                //设置约束
                [hangupButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.centerX.mas_equalTo(self);
                    make.width.mas_equalTo(60);
                    make.height.mas_equalTo(90);
                }];
                
                [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(hangupButton);
                    make.left.mas_equalTo(self).offset(30);
                    make.width.height.mas_equalTo(44);
                }];
                
                [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerY.mas_equalTo(hangupButton);
                    make.right.mas_equalTo(self).offset(-30);
                    make.width.height.mas_equalTo(44);
                }];
                
            }
                
            default:
                break;
        }
        
        
        //切换UI
        [self updateUIWithStatus:status];
    }
    return self;
}



/**根据状态切换UI*/
- (void)updateUIWithStatus:(GPVideoViewStatus )status
{
    self.status = status;
    
    switch (status) {
        case GPVideoViewStatusCalling:
        {
            //主叫
            self.cancelButton.hidden = NO;
            self.declineButton.hidden = YES;
            self.reveiveButton.hidden = YES;
        }
            break;
        case GPVideoViewStatusCalled:
        {
            //被叫
            self.cancelButton.hidden = YES;
            self.declineButton.hidden = NO;
            self.reveiveButton.hidden = NO;
        }
            break;
        case GPVideoViewStatusVideoSession:
        {
            //视频通话中
            [self.leftButton setImage:[UIImage imageNamed:@"changeToVoice"] forState:UIControlStateNormal];    //切换成语音
            [self.rightButton setImage:[UIImage imageNamed:@"ic_call_camer_switch"] forState:UIControlStateNormal]; //切换摄像头
        }
            break;
        case GPVideoViewStatusVoiceSession:
        {
            //音频通话中
            [self.leftButton setImage:[UIImage imageNamed:@"slienceVoice_off"] forState:UIControlStateNormal];//静音
            [self.rightButton setImage:[UIImage imageNamed:@"alound_off"] forState:UIControlStateNormal];   //免提
        }
            break;
        default:
            break;
    }

}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self updateUIWithStatus:self.status];
}



#pragma mark - action -- 未接通状态
//被叫 -- 拒绝接听
- (IBAction)rejectButtonClicked:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(toolView:status:didRejectCalled:)]) {
        [self.delegate toolView:self status:self.status didRejectCalled:sender];
    }
    
}
//被叫 -- 接听通话
- (IBAction)receiveButtonClicked:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(toolView:status:didReceiveCalled:)]) {
        [self.delegate toolView:self status:self.status didReceiveCalled:sender];
    }
    
}
//主叫 -- 取消主叫
- (IBAction)cancelButtonClicked:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(toolView:status:didCancelCalling:)]) {
        [self.delegate toolView:self status:self.status didCancelCalling:sender];
    }
    
}


#pragma mark - action -- 接通状态
//接通中 - 主动挂点
- (IBAction)hangupButtonClicked:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(toolView:status:didHangupSession:)]) {
        [self.delegate toolView:self status:self.status didHangupSession:sender];
    }
    
}
//接通中 - 左边操作
- (IBAction)leftButtonClicked:(id)sender {
    
    if (self.status == GPVideoViewStatusVideoSession) {
        //视频通话 - 切换语音
        if ([self.delegate respondsToSelector:@selector(toolView:status:didSwitchToVoice:)]) {
            [self.delegate toolView:self status:self.status didSwitchToVoice:sender];
        }
        
    }else{
        //音频通话 - 静音
        if ([self.delegate respondsToSelector:@selector(toolView:status:didSlienceVoice:isSlience:)]) {
            self.isSlience = !self.isSlience;
            if (self.isSlience) {
                //如果是静音
                [self.leftButton setImage:[UIImage imageNamed:@"slienceVoice_on"] forState:UIControlStateNormal];
            }else{
                //关闭静音
                [self.leftButton setImage:[UIImage imageNamed:@"slienceVoice_off"] forState:UIControlStateNormal];
            }
            [self.delegate toolView:self status:self.status didSlienceVoice:sender isSlience:self.isSlience];
        }
    }
    
    
}
//接通中 - 右边操作
- (IBAction)rightButtonClicked:(id)sender {
    
    if (self.status == GPVideoViewStatusVideoSession) {
        //视频通话 - 切换摄像头
        if ([self.delegate respondsToSelector:@selector(toolView:status:didChangeCamera:)]) {
            [self.delegate toolView:self status:self.status didChangeCamera:sender];
        }
        
    }else{
        //音频通话 - 免提
        if ([self.delegate respondsToSelector:@selector(toolView:status:didHandsOff:ishandsOff:)]) {
            self.isHandsOff = !self.isHandsOff;
            if (self.isHandsOff) {
                //开启免提
                [self.rightButton setImage:[UIImage imageNamed:@"alound_on"] forState:UIControlStateNormal];   //免提
            }else{
                //关闭免提
                [self.rightButton setImage:[UIImage imageNamed:@"alound_off"] forState:UIControlStateNormal];   //免提
            }
            [self.delegate toolView:self status:self.status didHandsOff:sender ishandsOff:self.isHandsOff];
        }
    }
    
}


@end
